class Node:
    def __init__(self, data = None):
        self.data = data
        self.next = None
        self.prev = None


class Shuanglinklist:
    def __init__(self):
        self.head = Node()
        self.tail = Node()
        self.length = 0

    def insert(self, pos, data):
        assert pos >= 0 and pos <= self.length
        # 设置一个游标 tmp
        tmp = self.head
        while pos > 0:
            tmp = tmp.next
            pos -= 1
            # self.lenth += 1

        # 开始考虑在某个位置 pos 插入了NewNode,data
        # 创建一个新的插入节点
        # newNode = Node()
        # newNode.data = data
        # newNode.next = None
        # newNode.prev = None
        #
        # newNode.next = tmp.next
        # tmp.next = newNode
        #
        # newNode.prev = tmp
        #
        # tmp.next.prev = newNode
        # self.lenth += 1

        newNode = Node()
        newNode.data = data

        curNode = tmp
        nextNode = tmp.next
        # 谁赋值给谁 位置 重要吗
        # newNode.next = curNode.next
        # newNode.prev = curNode

        # 老师的 双向 验证

        curNode.next = newNode
        newNode.prev = curNode

        newNode.next = nextNode # 我怎么感觉包含了为空的情况呢?
        #验证不在最后一个节点后加入
        if nextNode:
            nextNode.prev = newNode

        # 添加成功要长度+1
        self.length += 1

    def __iter__(self):
        tmp = self.head.next # 何时加 next 何时不加啊
        while self.head != self.tail:
            yield tmp.data
            tmp = tmp.next

    def __len__(self):
        # self.length = length
        return self.length

    def append(self, data):
        self.insert(self.length, data)


if __name__ == '__main__':
    l = Shuanglinklist()
    l.append(1)
    l.append(2)
    l.append(3)
    print(l) # <__main__.Shuanglinklist object at 0x102962518>
    # l.insert(1, 4)
    # for x in l:
    #     print(x)














